package com.Sdebruijn.curriculumvitae.models;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Wrapper for a task describing a job shortly
 * 
 * @author Stefan de Bruijn
 */
@DatabaseTable(tableName = "WorkTask")
public class WorkTask implements Parcelable {

	/**
	 * Auto-incremented id for the ORMLite-SQLite database
	 */
	@DatabaseField(generatedId = true)
	public int id;

	/**
	 * Foreign field id for the ORMLite-SQLite database
	 */
	@DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true, columnName = "work_id")
	public Work work;

	/**
	 * Description of the task
	 */
	@DatabaseField
	@SerializedName("description")
	public String description;

	/**
	 * Priority for the order
	 */
	@DatabaseField
	@SerializedName("priority")
	public int priority;

	WorkTask() {
		// all persisted classes must define a no-arg constructor with at least
		// package visibility
	}

	/*
	 * Required methods to have this class as Parcelable Having this class
	 * Parcelable will make it easier to restore it into fragments with
	 * savedInstanceState
	 */

	/*
	 * Just used as id - hashCode() will do fine
	 * 
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		return this.hashCode();
	}

	/*
	 * Serialize my object attributes one by one
	 * 
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeInt(this.id);
		parcel.writeString(this.description);
		parcel.writeInt(this.priority);
	}

	/**
	 * Required for any Parcelable class to be recreated. Will be called to
	 * de-serialize the object.
	 * 
	 * @author Stefan de Bruijn
	 * 
	 */
	public static final Parcelable.Creator<WorkTask> CREATOR = new Parcelable.Creator<WorkTask>() {
		public WorkTask createFromParcel(Parcel source) {
			return new WorkTask(source);
		}

		public WorkTask[] newArray(int size) {
			return new WorkTask[size];
		}
	};

	/**
	 * <b>Do not use!</b> Private constructor for the Parcelable Creator. Will
	 * de-serialize the object.
	 * 
	 * @param source
	 */
	private WorkTask(Parcel source) {
		this.id = source.readInt();
		this.description = source.readString();
		this.priority = source.readInt();
	}
}
